package com.dhcc.android.base.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;

/**
 * Created by eric on 2016/5/30.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected String TAG = this.getClass().getSimpleName();
    private Toolbar toolbar;
    private TextView toolbar_title;
    private onBackClickListener mOnBackClickListener;
    private Boolean isToolBarShow = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        toolbar.setTitle("");
    }

    public void setContentView(int layoutId) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(layoutId, null);
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.contentArea);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        frameLayout.addView(content, -1, layoutParams);
        initToolBar();
    }

    protected void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        if (isToolbarShow()) {
            setTitle(toolbar_title);
            setSupportActionBar(toolbar);
            setToolBarNavigationIcon(toolbar);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnBackClickListener != null)
                        mOnBackClickListener.onBackClick();
                    else
                        BaseActivity.this.finish();
                }
            });
        } else {
            toolbar.setVisibility(View.GONE);
        }
        initView();
    }

    protected abstract void setTitle(TextView toolbar_title);

    protected abstract void setToolBarNavigationIcon(Toolbar toolbar);

    protected abstract void initView();

    protected boolean isToolbarShow() {
        return isToolBarShow;
    }

    public void setToolBarShow(Boolean toolBarShow) {
        isToolBarShow = toolBarShow;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void setmOnBackClickListener(onBackClickListener mOnBackClickListener) {
        this.mOnBackClickListener = mOnBackClickListener;
    }

    public void setNavigationOnClickListener(View.OnClickListener onClickListener) {
        toolbar.setNavigationOnClickListener(onClickListener);
    }

    public interface onBackClickListener {
        void onBackClick();
    }

    public void setTitle(String title) {
        toolbar_title.setText(title);
    }

    public String getToolbarTitle() {
        return toolbar_title.getText().toString();
    }
}
